package com.jinlejia.erp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ItemFormLayoutBinding;

import java.util.ArrayList;
import java.util.List;


/**
 * 表格适配器
 */

public class FormAdapter extends YfListAdapter<String> {


    private Context context;
    private ArrayList<String> tag = new ArrayList<>();
    private ArrayList<String> hasCheck = new ArrayList<>();

    public FormAdapter(ArrayList<String> data) {
        super(data);
    }

    public void setHasCheck(ArrayList<String> data) {
        this.hasCheck = data;
    }


    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_form_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);

        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, final int i) {
        String tagStr = mData.get(i);
        final ItemFormLayoutBinding binding = DataBindingUtil.getBinding(viewHolder.itemView);
        binding.tag.setText(tagStr);
        if (hasCheck.size() > 0 && hasCheck.contains(tagStr)) {
            binding.tag.setChecked(true);
            hasCheck.remove(tagStr);
            tag.add(tagStr);
        }

        binding.tag.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    String s = binding.tag.getText().toString();
                    tag.add(s);
                    listiner.Onselected(tag);
                } else {
                    String s = binding.tag.getText().toString();
                    tag.remove(s);
                    listiner.Onselected(tag);
                }
            }
        });
        binding.getRoot().setTag(mData.get(i));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new YfSimpleViewHolder(view);
    }


    static final class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }

    public interface OnSelectedListiner {
        void Onselected(List<String> list);
    }

    private OnSelectedListiner listiner;

    public void setOnSelectedListiner(OnSelectedListiner listiner) {
        this.listiner = listiner;
    }
}
